\documentclass{article}
\usepackage{amsmath}
\usepackage{amssymb}
\usepackage{amsfonts}
\usepackage{graphicx} 
\usepackage[left=1cm,right=1cm,top=1cm,bottom=2cm]{geometry}

\geometry{a4paper, margin=1in}

\title{Design and Implementation of Poisson Solver}
\author{3220101339 Jiang Zhou}
\date{2025/3/17}

\begin{document}

\maketitle

\section{Introduction}
Enter \textbf{make} in the terminal to get the error, convergence rate, image result, and the file \textbf{report.pdf}.
This report outlines the design and implementation of Poisson solver, as encapsulated in the \texttt{poisson.hpp} file. The solver is designed to handle various boundary conditions and domain configurations, including square domains and square domains with a circular hole. The solver employs finite difference methods and leverages the Eigen library for sparse matrix operations.

\section{Domain and Boundary Conditions}
The solver supports two types of domains:
\begin{itemize}
    \item \textbf{Square Domain}: A unit square domain defined by \(0 \leq x \leq 1\) and \(0 \leq y \leq 1\).
    \item \textbf{Square Domain with a Circular Hole}: A unit square domain with a circular hole centered at \((Point\_x, Point\_y)\) with a specified radius.
\end{itemize}

Boundary conditions can be of three types:
\begin{itemize}
    \item \textbf{Dirichlet}: Specifies the value of the solution on the boundary.
    \item \textbf{Neumann}: Specifies the derivative of the solution on the boundary.
    \item \textbf{Mixed}: A combination of Dirichlet and Neumann conditions on different parts of the boundary.
\end{itemize}

\section{Design Overview}
The solver is implemented in C++ and uses the Eigen library for handling sparse matrices. The main components of the solver are:

\subsection{Domain Parameters}
The \texttt{DomainParams} struct encapsulates the domain configuration, including the type of domain (square or square with a hole), the center and radius of the hole (if applicable), and the grid resolution.

\subsection{Boundary Conditions}
The \texttt{BoundaryConditions} struct defines the boundary conditions for the problem. It includes flags to specify the type of boundary condition on each edge of the domain and function pointers to evaluate the boundary conditions.

\subsection{Interpretation of some parameters}
\subsubsection*{DomainParams structure}
The DomainParams structure defines the problem domain and its properties.
\begin{itemize}
    \item \(\text{Type}: \text{SQUARE} \mid \text{SQUARE\_MINUS\_DISK}\)
    \item \text{Point\_x}, \text{Point\_y}, \text{radius}: \text{Center and radius of the disk}
    \item \text{grid\_resolution}: \text{Number of grid points along each dimension}
\end{itemize}

\subsubsection*{BoundaryConditions structure}
The BoundaryConditions structure defines the boundary conditions for the problem.
\begin{itemize}
    \item \text{outer\_bc}, \text{inner\_bc}: \text{DIRICHLET} $\mid$ \text{NEUMANN} $\mid$ \text{Mixed}
    \item \text{flag\_outer\_u}: \text{Flags for outer boundary conditions}
    \item \text{one\_point\_loc}: \text{Location of a single Dirichlet point}
    \item \text{one\_point\_value}: \text{Value at the Dirichlet point}
    \item \begin{align*}
        u_x, u_y &: \text{Functions for Neumann conditions} \\
        u_1, u_2, u_3, u_4 &: \text{Functions for Dirichlet conditions on each side}
        \end{align*}
    \item \begin{align*}
        \text{inner\_u} &: \text{General boundary condition function for inner boundary} \\
        \text{inner\_u\_x}, \text{inner\_u\_y} &: \text{Functions for Neumann conditions on inner boundary}
        \end{align*}
    \item \text{flag\_inner\_u}: \text{Flag for inner boundary condition type}
\end{itemize}

\subsection{PoissonSolver Class}
The \texttt{PoissonSolver} class is the core of the solver. It contains methods to build the system of equations, solve the system, and retrieve the solution. The class also includes helper methods to validate the domain and handle irregular points near the boundary of the circular hole.

\section{Implementation Details}

\subsection{Building the System}
The \texttt{Build\_System} method constructs the sparse matrix \(A\) and the right-hand side vector \(F\) based on the domain and boundary conditions. The method handles different types of boundary conditions and ensures that the system is correctly assembled for both regular and irregular points.

\subsection{Solving the System}
The \texttt{Solve} method uses Eigen's sparse matrix solver to solve the system \(AU = F\). The solution is stored in the vector \(U\), which can be retrieved using the \texttt{GetSolution} method.

\subsection{Handling Irregular Points}
For domains with a circular hole, the solver identifies irregular points near the boundary of the hole and adjusts the finite difference stencils accordingly. This ensures that the solution is accurate even near the irregular boundary.

\section{Test on different functions}
\subsection{\(u(x,y) = e^{y+\sin x}\)}
In \texttt{Function1}, we test the Poisson solver with the function \(u(x, y) = e^{y + \sin x}\). The error and convergence rate with different norm are output in the terminal.
\begin{figure}
    \centering
    \begin{minipage}{0.45\linewidth}
        \centering
        \includegraphics[width=\linewidth]{figure/F1_Square_D.png}
        \caption{Function1 Square Dirichlet}
        \label{F1_S_D}
    \end{minipage}\hfill
    \begin{minipage}{0.45\linewidth}
        \centering
        \includegraphics[width=\linewidth]{figure/F1_Square_N.png}
        \caption{Function1 Square Neumann}
        \label{F1_S_N}
    \end{minipage}
\end{figure}


\begin{figure}
    \centering
    \begin{minipage}{0.45\linewidth}
        \centering
        \includegraphics[width=\linewidth]{figure/F1_Square_M.png}
        \caption{Function1 Square Mixed}
        \label{F1_S_M}
    \end{minipage}\hfill
    \begin{minipage}{0.45\linewidth}
        \centering
        \includegraphics[width=\linewidth]{figure/F1_Disk_D.png}
        \caption{Function1 Square Without Disk Dirichlet}
        \label{F1_D_D}
    \end{minipage}
\end{figure}

\begin{figure}
    \centering
    \begin{minipage}{0.45\linewidth}
        \centering
        \includegraphics[width=\linewidth]{figure/F1_Disk_N.png}
        \caption{Function1 Square Without Disk Neumann}
        \label{F1_D_N}
    \end{minipage}\hfill
    \begin{minipage}{0.45\linewidth}
        \centering
        \includegraphics[width=\linewidth]{figure/F1_Disk_M.png}
        \caption{Function1 Square Without Disk Mixed}
        \label{F1_D_M}
    \end{minipage}
\end{figure}

\subsection{\(u(x,y) = \sin(\pi x)\sin(\pi y)\)}
In \texttt{Function2}, we test the Poisson solver with the function \(u(x,y) = \sin(\pi x)\sin(\pi y)\). The error and convergence rate with different norm are output in the terminal.
\begin{figure}
    \centering
    \begin{minipage}{0.45\linewidth}
        \centering
        \includegraphics[width=\linewidth]{figure/F2_Square_D.png}
        \caption{Function2 Square Dirichlet}
    \end{minipage}\hfill
    \begin{minipage}{0.45\linewidth}
        \centering
        \includegraphics[width=\linewidth]{figure/F2_Square_N.png}
        \caption{Function2 Square Neumann}
    \end{minipage}
\end{figure}


\begin{figure}
    \centering
    \begin{minipage}{0.45\linewidth}
        \centering
        \includegraphics[width=\linewidth]{figure/F2_Square_M.png}
        \caption{Function2 Square Mixed}
    \end{minipage}\hfill
    \begin{minipage}{0.45\linewidth}
        \centering
        \includegraphics[width=\linewidth]{figure/F2_Disk_D.png}
        \caption{Function2 Square Without Disk Dirichlet}
    \end{minipage}
\end{figure}

\begin{figure}
    \centering
    \begin{minipage}{0.45\linewidth}
        \centering
        \includegraphics[width=\linewidth]{figure/F2_Disk_N.png}
        \caption{Function2 Square Without Disk Neumann}
    \end{minipage}\hfill
    \begin{minipage}{0.45\linewidth}
        \centering
        \includegraphics[width=\linewidth]{figure/F2_Disk_M.png}
        \caption{Function2 Square Without Disk Mixed}
    \end{minipage}
\end{figure}


\subsection{\(u(x,y) = e^{\sin x + \cos y}\)}
In \texttt{Function3}, we test the Poisson solver with the function \(u(x,y) = e^{\sin x + \cos y}\). The error and convergence rate with different norm are output in the terminal.
\begin{figure}
    \centering
    \begin{minipage}{0.45\linewidth}
        \centering
        \includegraphics[width=\linewidth]{figure/F3_Square_D.png}
        \caption{Function3 Square Dirichlet}
    \end{minipage}\hfill
    \begin{minipage}{0.45\linewidth}
        \centering
        \includegraphics[width=\linewidth]{figure/F3_Square_N.png}
        \caption{Function3 Square Neumann}
    \end{minipage}
\end{figure}


\begin{figure}
    \centering
    \begin{minipage}{0.45\linewidth}
        \centering
        \includegraphics[width=\linewidth]{figure/F3_Square_M.png}
        \caption{Function3 Square Mixed}
    \end{minipage}\hfill
    \begin{minipage}{0.45\linewidth}
        \centering
        \includegraphics[width=\linewidth]{figure/F3_Disk_D.png}
        \caption{Function3 Square Without Disk Dirichlet}
    \end{minipage}
\end{figure}

\begin{figure}
    \centering
    \begin{minipage}{0.45\linewidth}
        \centering
        \includegraphics[width=\linewidth]{figure/F3_Disk_N.png}
        \caption{Function3 Square Without Disk Neumann}
    \end{minipage}\hfill
    \begin{minipage}{0.45\linewidth}
        \centering
        \includegraphics[width=\linewidth]{figure/F3_Disk_M.png}
        \caption{Function3 Square Without Disk Mixed}
    \end{minipage}
\end{figure}
\end{document}